package com.base.cn.platform.os.controller.course.cou.packageCou;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.cou.packageCou.PackageCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 套餐管理Controller
 *
 * @author s.li
 * @create 2018-04-19-10:34
 */
@Controller
@RequestMapping("/admin/course/package")
public class AdminPackageController extends AdminBaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private PackageCourseService packageCourseService;

    /**
     * 初始化套餐课程列表
     * @return 列表页面
     */
    @RequestMapping("/toPackageCourseList")
    public String toPackageCourseList(){
        return resultPage("/course/cou/packageCou/package_course_list");
    }
    /**
     * 初始化套餐课程列表
     * @return 列表页面
     */
    @RequestMapping("/selectPackageCourseList")
    public String selectPackageCourseList(Model model,
                                          @RequestParam(value = "selectType",required = false,defaultValue = "radio") String selectType,
                                          @RequestParam(value = "subjectIds", required = false, defaultValue = "") String subjectIds){
        model.addAttribute("selectType",selectType);
        model.addAttribute("subjectIds", subjectIds);
        return resultPage("/course/cou/packageCou/select-package-list");
    }

    /**
     * 获取套餐课程数据
     * @param params
     * @param currentPage 当前页码
     * @return 列表页面
     */
    @RequestMapping("/selectPackageCourseListPage")
    public String selectPackageCourseListPage(Model model,
                                 @RequestBody Map<String,Object> params,
                                 @RequestParam(value = "currentPage",required = false,defaultValue = "0") int currentPage){
        params.put("courseTypeKey", CourseType.COURSE_TYPE_PACKAGE.getKey());
        params.put("type",1);
        Map<String,Object> dataMap = courseService.findCoursePage(params,currentPage,10,false,false,true);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.get("selectType"));
        return resultPage("/course/cou/packageCou/select-package-ajax-list");
    }


    /**
     * 获取套餐课程数据
     * @param params
     * @param currentPage 当前页码
     * @return 列表页面
     */
    @RequestMapping("/ajaxPackageCourseList")
    public String ajaxCourseList(Model model,
                                 @RequestBody Map<String,Object> params,
                                 @RequestParam(value = "currentPage",required = false,defaultValue = "0") int currentPage){
        params.put("courseTypeKey",CourseType.COURSE_TYPE_PACKAGE.getKey());
        params.put("type",1);
        Map<String,Object> dataMap = courseService.findCoursePage(params,currentPage,12,false,false,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/course/cou/packageCou/ajax_package_course_list");
    }

    /**
     * 设置套餐课程页面
     * @param packageId 套餐ID
     * @return 操作页面
     */
    @RequestMapping("/initPackageCoursePage")
    public String initPackageCoursePage(Model model,
                                        @RequestParam("packageId") BigDecimal packageId){
        Map<String,Object> course = courseService.findCourseById(packageId,false,false,false, false);
        model.addAttribute("course",course);
        return resultPage("/course/cou/packageCou/package-course");
    }

    /**
     * 加载套餐课程数据
     * @param packageId 套餐ID
     * @return 显示页面
     */
    @RequestMapping("/ajaxPackageCourse")
    public String ajaxPackageCourse(Model model,
                                    @RequestParam("status") int status,
                                    @RequestParam("packageId") BigDecimal packageId){
        Map<Integer,List<Map<String,Object>>> packageCourseListMap = packageCourseService.findLayeredPackageCourseByPackageId(packageId,true);
        model.addAttribute("packageCourseListMap",packageCourseListMap);
        model.addAttribute("status",status);
        return resultPage("/course/cou/packageCou/ajax-package-course");
    }

    /**
     * 创建阶段数据
     * @param packageId 套餐ID
     * @param stageContext 阶段描述内容
     * @return Map<String,Object>
     */
    @RequestMapping("/createStage")
    @ResponseBody
    public Map<String,Object> createStage(@RequestParam("packageId") BigDecimal packageId,
                                     @RequestParam("stageContext") String stageContext){
        return packageCourseService.createStage(packageId,stageContext);
    }

    /**
     * 添加套餐课程
     * @param parentId 父ID
     * @param packageId 套餐ID
     * @param courseIds 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/savePackageCourse")
    @ResponseBody
    public Map<String,Object> savePackageCourse(@RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                                @RequestParam("packageId") BigDecimal packageId,
                                                @RequestParam("courseIds") String courseIds){
        return packageCourseService.savePackageCourse(parentId,packageId,courseIds);
    }

    /**
     * 修改套餐关联课程是否为必须
     * @param id 记录ID
     * @param required 是否为必选课程，1.必选 2.非必选
     * @return Map<String,Object>
     */
    @RequestMapping("/updatePackageCourseRequired")
    @ResponseBody
    public Map<String,Object> updatePackageCourseRequired(@RequestParam("id") BigDecimal id,
                                                          @RequestParam("required") int required){
        return packageCourseService.updatePackageCourseRequired(id,required);
    }

    /**
     * 修改套餐关联的课程的价格
     * @param id 记录ID
     * @param price 价格
     * @return Map<String,Object>
     */
    @RequestMapping("/updatePackagePrice")
    @ResponseBody
    public Map<String,Object> updatePackagePrice(@RequestParam("id") BigDecimal id,@RequestParam("price") Double price){
        return packageCourseService.updatePackageCoursePrice(id,price);
    }

    /**
     * 修改套餐与课程关联的排序
     * @param id
     * @param sort
     * @return
     */
    @RequestMapping("/updatePackageCourseSort")
    @ResponseBody
    public Map<String,Object> updatePackageCourseSort(@RequestParam("id") BigDecimal id,@RequestParam("sort") Integer sort){
        return packageCourseService.updatePackageCourseSort(id,sort);
    }

    /**
     * 删除套餐下的课程
     * @param packageId 套餐ID
     * @param ids 记录ID
     * @return Map<String,Object>
     */
    @RequestMapping("/deletePackageCourse")
    @ResponseBody
    public Map<String,Object> deletePackageCourse(@RequestParam("packageId") BigDecimal packageId,
                                                  @RequestParam("ids") String ids){
        return packageCourseService.deletePackageCourse(packageId,ids);
    }


}
